package facilius.controller;

import facilius.controller.action.LoadFileAction;
import facilius.controller.action.RemoveFileAction;
import facilius.controller.actionmanager.CursoActionManager;
import facilius.controller.actionmanager.AlunoCursoActionManager;
import facilius.controller.actionmanager.AulaActionManager;
import facilius.controller.actionmanager.DisciplinaActionManager;
import facilius.controller.actionmanager.EspacoActionManager;
import facilius.controller.actionmanager.FrequenciaActionManager;
import facilius.controller.actionmanager.InstituicaoActionManager;
import facilius.controller.actionmanager.MaterialActionManager;
import facilius.controller.actionmanager.NotaActionManager;
import facilius.controller.actionmanager.TipoNotaActionManager;
import facilius.controller.actionmanager.TurmaActionManager;
import facilius.controller.actionmanager.UsuarioActionManager;
import facilius.controller.actionmanager.UsuarioCursoTurmaActionManager;
import java.util.List;
import org.mentawai.authorization.AuthorizationManager;
import org.mentawai.authorization.Group;
import org.mentawai.core.ActionConfig;
import org.mentawai.core.Forward;
import org.mentawai.core.Redirect;
import org.mentawai.core.StreamConsequence;
import org.mentawai.filter.AuthenticationFilter;
import org.mentawai.filter.AuthorizationFilter;

/**
 *
 * @author Graciano
 */
public class ApplicationManager extends org.mentawai.core.ApplicationManager {

    @Override
    public void init() {
        GroupManager.getInstance();

        //Adicionando os grupos
        List<Group> grupos = GroupManager.getInstance().getGroups();
        for (int i = 0; i < grupos.size(); i++) {
            AuthorizationManager.addGroup(grupos.get(i));
        }
    }

    @Override
    public void loadActions() {

        //para setar toda a aplicação a trabalhar com este encoding
        this.setReqCharEncoding("UTF-8");

        addGlobalFilter(new AuthenticationFilter());//só consegue acessar a action quem tiver sido logado
        addGlobalConsequence(AuthenticationFilter.LOGIN, new Redirect("Login.mtw"));
        addGlobalConsequence(AuthorizationFilter.ACCESSDENIED, new Redirect("Logout.mtw"));

        addGlobalConsequence(EXCEPTION, new Forward("jsp/erro.page"));

        ActionConfig ac = null;

        ac = new ActionConfig("LoadFile", LoadFileAction.class);
        ac.addConsequence(SUCCESS, new StreamConsequence());
        ac.addConsequence(ERROR, new Forward("jsp/erro.page"));
        this.addActionConfig(ac);

        ac = new ActionConfig("RemoveFile", RemoveFileAction.class);
        ac.addConsequence(SUCCESS, new Redirect("AulaRead.mtw"));
        ac.addConsequence(ERROR, new Forward("jsp/erro.page"));
        this.addActionConfig(ac);

        new TipoNotaActionManager().loadActions(this);
        new CursoActionManager().loadActions(this);
        new DisciplinaActionManager().loadActions(this);
        new NotaActionManager().loadActions(this);
        new UsuarioActionManager().loadActions(this);
        new InstituicaoActionManager().loadActions(this);
        new TurmaActionManager().loadActions(this);
        new UsuarioCursoTurmaActionManager().loadActions(this);
        new AlunoCursoActionManager().loadActions(this);
        new AulaActionManager().loadActions(this);
        new FrequenciaActionManager().loadActions(this);
        new EspacoActionManager().loadActions(this);
        new MaterialActionManager().loadActions(this);

    }
}
